package com.aeon.asm;

import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import static org.objectweb.asm.Opcodes.*;

public class PerfMethodAdapter extends MethodVisitor {
	private String className, methodName;

	public PerfMethodAdapter(MethodVisitor visitor, String className,
			String methodName) {
		super(ASM4, visitor);
		this.className = className;
		this.methodName = methodName;
	}

	public void visitCode() {
		this.visitLdcInsn(className);
		this.visitLdcInsn(methodName);
		this.visitMethodInsn(INVOKESTATIC, "com/aeon/profiler/TimeProfile",
				"start", "(Ljava/lang/String;Ljava/lang/String;)V");
		super.visitCode();
	}

	public void visitInsn(int inst) {
		switch (inst) {
		case Opcodes.ARETURN:
		case Opcodes.DRETURN:
		case Opcodes.FRETURN:
		case Opcodes.IRETURN:
		case Opcodes.LRETURN:
		case Opcodes.RETURN:
		case Opcodes.ATHROW:
			this.visitLdcInsn(className);
			this.visitLdcInsn(methodName);
			this.visitMethodInsn(INVOKESTATIC, "com/aeon/profiler/TimeProfile",
					"end", "(Ljava/lang/String;Ljava/lang/String;)V");
			break;
		default:
			break;
		}

		super.visitInsn(inst);
	}
}